<template>
  <div class="carousel-item-container" ref="container" @mousemove="handleMouseMove" @mouseleave="handleMouseLeave">
    <div class="carousel-img" ref="image" :style="imagePosition">
      <ImageLoader 
        @load="this.showWords"
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
      />
    </div>
    <div class="title" ref="title">{{carousel.title}}</div>
    <div class="desc" ref="desc">{{carousel.description}}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";
export default {
  components:{
    ImageLoader,
  },
  props:["carousel"],
  data(){
    return{
      titleWidth:0,
      descWidth:0,
      containerSize:null,
      innerSize:null,
      mouseX:0,
      mouseY:0,
    };
  },
  computed:{
    // 得到图片坐标
    imagePosition(){
      if(!this.innerSize||!this.containerSize){
        return {};
      };
      const extraWidth = this.innerSize.width-this.containerSize.width;//多出的宽度
      const extraHeight = this.innerSize.height-this.containerSize.height;//多出的宽度
      const left = (-extraWidth/this.containerSize.width)*this.mouseX;
      const top = (-extraHeight/this.containerSize.height)*this.mouseY;
      return {
        transfrom:`translate(${left}px,${top}px)`,
      };
    },
    // 中心点坐标
    center(){
      return{
        x:this.containerSize.width/2,
        y:this.containerSize.height/2,
      };
    },
  },
  mounted(){
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.setSize();
    this.mouseX=this.center.x;
    this.mouseY=this.center.y;
    window.addEventListener("resize",this.setSize);
  },
  destroyed(){
    window.removeEventListener("resize",this.setSize);
  },
  methods:{
    //显示文字
    showWords(){
      this.$refs.title.style.opacity=1;
      this.$refs.title.style.width=0;
      this.$refs.title.clientWidth;//强制渲染
      this.$refs.title.style.transition="1s";
      this.$refs.title.style.width=this.titleWidth+"px";
      this.$refs.desc.style.opacity=1;
      this.$refs.desc.style.width=0;
      this.$refs.desc.clientWidth;//强制渲染
      this.$refs.desc.style.transition="2s 1s";
      this.$refs.desc.style.width=this.descWidth+"px";
    },
    //设置尺寸
    setSize(){
      this.containerSize={
        width:this.$refs.container.clientWidth,
        height:this.$refs.container.clientHeight,
      },
      this.innerSize={
        width:this.$refs.image.clientWidth,
        height:this.$refs.image.clientHeight,
      }
    },
    handleMouseMove(e){
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX=e.clientX-rect.left;
      this.mouseY=e.clientY-rect.top;
    },
    handleMouseLeave(){
      this.mouseX=this.center.x;
      this.mouseY=this.center.y;
    },
  },
};
</script>

<style scoped lang="less">
@import "~@/styles/var.less";
.carousel-item-container{
    background-color: @dark;
    width: 100%;
    height: 100%;
    color: #fff;
    position: relative;
    overflow: hidden;
}
.carousel-img{
  width: 110%;
  height: 110%;
  position: absolute;
  left: 0;
  top: 0;
  transition: .3s;
}
.title,
.desc{
  position: absolute;
  letter-spacing: 3px;
  left: 30px;
  color: #fff;
  opacity: 0;
  text-shadow: 1px 0 0 rgba(0, 0, 0, .5),
              0 1px 0 rgba(0, 0, 0, .5),
              -1px 0 0 rgba(0, 0, 0, .5),
              0 -1px 0 rgba(0, 0, 0, .5);
  white-space: nowrap;
  overflow: hidden;
}
.title{
  top: calc((60% - 40px));
}
.desc{
  top: calc(60% + 20px);
  font-size: 1.2em;
  color: lighten(@gray,20%);
}
</style>